// Copyright Rivtower Technologies LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use core::libchain::chain::Chain;
use libproto::executor::ExecutedResult;
use pubsub::channel::Sender;
use std::sync::Arc;

/// Processing blocks and transaction storage
#[derive(Clone)]
pub struct BlockProcessor {
    pub chain: Arc<Chain>,
    ctx_pub: Sender<(String, Vec<u8>)>,
}

impl BlockProcessor {
    pub fn new(chain: Arc<Chain>, ctx_pub: Sender<(String, Vec<u8>)>) -> Self {
        BlockProcessor { chain, ctx_pub }
    }

    pub fn broadcast_current_status(&self) {
        self.chain.broadcast_current_status(&self.ctx_pub);
    }

    pub fn set_executed_result(&self, ret: &ExecutedResult) {
        self.chain.set_executed_result(ret, &self.ctx_pub);
    }

    pub fn reset_max_store_height(&self) {
        self.chain
            .set_max_store_height(self.chain.get_current_height());
    }

    pub fn signal_to_executor(&self) {
        self.chain.signal_to_executor(&self.ctx_pub)
    }
}
